import seal from '@/lib/request/seal'
import { RootState } from '../index'
import { Commit } from 'vuex'
import { delay } from '@/lib/utils'
import { ISealData } from '@/store'
import { Message } from 'element-ui'
import { IResponse } from '@/lib/service'

interface Context {
  commit: Commit
  state: NewApplyState
  rootState: RootState
  getters: any
}

export interface NewApplyState {
  sealList: any[]
  total: number
  applyVisible: boolean
  detailVisible: boolean
  loading: false
  orgList: any[]
  auditOrgList: any[]
  applyData?: any
  applyInvoiceVisible: boolean
  applyInvoiceData?: any
  refundDialogVisible: boolean
  refundData?: any
}
const initState: NewApplyState = {
  sealList: [],
  total: 0,
  applyVisible: false,
  detailVisible: false,
  loading: false,
  orgList: [],
  auditOrgList: [],
  applyData: null,
  applyInvoiceVisible: false,
  applyInvoiceData: {},
  refundDialogVisible: false,
  refundData: {},
}

const getters = {}

const actions = {
  async getList(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    context.commit('setStateData', { loading: true })
    const data: IResponse = await seal
      .querySealApplyByAgent(
        payload.pageIndex,
        payload.pageSize,
        payload.sealName,
        payload.status,
        payload.sealType,
        1,
        payload.sealApplyId
      )
      .catch((e: any) => {
        context.commit('setStateData', {
          loading: false,
          sealList: [],
          total: 0,
        })
      })
    context.commit('setStateData', { loading: false })
    if (data && data.data) {
      context.commit('setStateData', {
        sealList: data.data.list || [],
        total: data.data.total,
      })
    }
  },
  queryOrg(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    if (!payload) return
    delay(async () => {
      const data: IResponse = await seal.queryOrg(payload)
      if (data && data.data) {
        context.commit('setStateData', {
          orgList: (data.data.list || []).map((item: any, index: number) => {
            return {
              organizeName: item.fullName,
              organizeUUID: item.uuid,
              organizeAdministrationLevel: item.administrationLevel,
              organizeLicenseNumber: item.licenseNumber || item.organizeCode,
              organizeLicenseType: item.licenseType,
              organizeType: item.organizeType,
            }
          }),
        })
      }
    }, 200)
  },
  queryAuditOrg(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    if (!payload) return
    delay(async () => {
      const data: IResponse = await seal.queryAuditOrg(payload)
      if (data && data.data) {
        context.commit('setStateData', {
          auditOrgList: (data.data.list || []).map(
            (item: any, index: number) => {
              return {
                organizeName: item.fullName,
                organizeUUID: item.uuid,
                organizeAdministrationLevel: item.administrationLevel,
                organizeLicenseNumber: item.licenseNumber || item.organizeCode,
                organizeLicenseType: item.licenseType,
                organizeType: item.organizeType,
              }
            }
          ),
        })
      }
    }, 200)
  },
  saveSeal(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: ISealData
  ) {
    return new Promise(async (resolve, reject) => {
      await seal
        .sealApply(
          payload.organizeUUID,
          payload.auditOrganizeUUID,
          payload.fileKey,
          payload.sealName,
          payload.sealType,
          payload.useType,
          payload.agentLicenseNumber,
          payload.agentName,
          payload.agentMobile,
          payload.organizeLicenseNumber,
          payload.organizeName,
          payload.auditOrganizeLicenseNumber,
          payload.auditOrganizeName,
          payload.agentLicenseType,
          payload.organizeAdministrationLevel,
          payload.organizeLicenseType,
          payload.organizeType,
          payload.auditOrganizeLicenseType,
          payload.sealApplyId,
          payload.sealUseTime,
          payload.licenseNumberSource,
          payload.licenseNumberFileKey,
          payload.sealElectronicPicFileKey,
          payload.sealElectronicPicSubmitType
        )
        .then((data: IResponse) => {
          Message.success('保存成功')
          context.commit('setStateData', {
            applyData: null,
            applyVisible: false,
          })
          resolve(data)
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  saveAndSubmit(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: ISealData
  ) {
    return new Promise(async (resolve, reject) => {
      seal
        .sealApplySaveAndSubmit(
          payload.organizeUUID,
          payload.auditOrganizeUUID,
          payload.fileKey,
          payload.sealName,
          payload.sealType,
          payload.useType,
          payload.agentLicenseNumber,
          payload.agentName,
          payload.agentMobile,
          payload.organizeLicenseNumber,
          payload.organizeName,
          payload.auditOrganizeLicenseNumber,
          payload.auditOrganizeName,
          payload.agentLicenseType,
          payload.organizeAdministrationLevel,
          payload.organizeLicenseType,
          payload.organizeType,
          payload.auditOrganizeLicenseType,
          payload.sealApplyId
        )
        .then((data: IResponse) => {
          // Message.success('保存并提交成功');
          context.commit('setStateData', {
            applyData: null,
            applyVisible: false,
          })
          resolve(data)
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  submitSeal(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise(async (resolve, reject) => {
      seal
        .sealApplySubmit(payload)
        .then((data: IResponse) => {
          Message.success('提交成功')
          resolve(data)
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  sealDetail(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise((resolve, reject) => {
      seal.querySealApplyDetail(payload).then((res: IResponse) => {
        resolve(res.data)
      })
    })
  },
  deleteApply(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise((resolve, reject) => {
      seal.deleteApply(payload).then((data: IResponse) => {
        resolve()
      })
    })
  },
  sealRecall(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return new Promise((resolve, reject) => {
      seal.sealRecall(payload).then((data: IResponse) => {
        resolve()
      })
    })
  },
  sealRefund(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: { sealApplyId: number; reasonType: number; otherReason: string }
  ) {
    return new Promise((resolve, reject) => {
      seal
        .sealRefund(
          payload.sealApplyId,
          payload.reasonType,
          payload.otherReason
        )
        .then((data: IResponse) => {
          resolve()
        })
        .catch(e => {
          reject(e)
        })
    })
  },
  sealApplyPicSubmit(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: { fileKey: string; sealApplyId: number }
  ) {
    return seal.sealApplyPicSubmit(payload.fileKey, payload.sealApplyId)
  },
  receiptConfirm(
    context: {
      commit: Commit
      state: NewApplyState
      rootState: RootState
      getters: any
    },
    payload: number
  ) {
    return seal.receiptConfirm(payload)
  },
  checkSealNameRepeat(context: Context, payload: any) {
    return seal.checkSealNameRepeat(payload)
  },
  querySealApplyOps(context: Context, payload: any) {
    return seal.querySealApplyOps(payload)
  },
}

const mutations = {
  setStateData(state: NewApplyState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
